package com.yzcrm.erp.kms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.paragetter.Para;
import com.yzcrm.common.annotation.NotNullValidate;
import com.yzcrm.common.annotation.Permissions;
import com.yzcrm.common.config.paragetter.BasePageRequest;
import com.yzcrm.erp.admin.service.AdminFieldService;
import com.yzcrm.erp.crm.common.CrmEnum;
import com.yzcrm.erp.kms.service.KmsCourseService;
import com.yzcrm.swagger.annotation.ActionApi;
import com.yzcrm.swagger.annotation.Api;
import com.yzcrm.swagger.annotation.Param;
import com.yzcrm.swagger.annotation.Params;
import com.yzcrm.utils.R;

/**
 * @author jiang.xue
 * 课程管理
 */
@Api(remark = "知识库：课程管理")
public class KmsCourseController extends Controller{

    @Inject
    private AdminFieldService adminFieldService;

    @Inject
    private KmsCourseService courseService;

    /**
     * 查看列表页
     */
    @Permissions({"kms:course:index"})
    @ActionApi(summary = "课程列表数据", remark="课程列表数据")
    @Params({
            @Param(name="sceneId", remark="场景Id", dataType="int", defaultValue="", required=false),
            @Param(name="type", remark="权限标识 102：课程（course）", dataType="int", defaultValue="", required=true)
    })
    public void queryPageList(BasePageRequest basePageRequest) {

        JSONObject jsonObject = basePageRequest.getJsonObject().fluentPut("type", CrmEnum.KMS_COURSE.getType());
        basePageRequest.setJsonObject(jsonObject);
        renderJson(courseService.filterConditionAndGetPageList(basePageRequest));
    }

    /**
     * 添加或者修改课程
     */
    @ActionApi(summary = "添加或者修改课程", remark="添加或者修改课程")
    @Permissions({"kms:course:save", "kms:course:update"})
    public void saveOrUpdate(){

        JSONObject jsonObject = JSON.parseObject(getRawData());
        renderJson(courseService.addOrUpdate(jsonObject));
    }

    /**
     * 根据id删除课程
     */
    @ActionApi(summary = "根据id删除课程", remark="根据id删除课程")
    @Params({
            @Param(name="courseIds", remark="课程ID 多个ID,逗号分割", dataType="String", defaultValue="", required=true)
    })
    @Permissions("kms:course:delete")
    @NotNullValidate(value = "courseIds", message = "课程id不能为空")
    public void deleteByIds(@Para("courseIds") String courseIds) {
        renderJson(courseService.deleteByIds(courseIds));
    }

    /**
     * 根据课程id查询
     */
    @ActionApi(summary = "根据id查询课程", remark="根据id查询课程")
    @Params({
            @Param(name="courseId", remark="课程ID", dataType="String", defaultValue="", required=true)
    })
    @Permissions("kms:course:read")
    @NotNullValidate(value = "courseId", message = "课程id不能为空")
    public void queryById(@Para("courseId") Integer courseId) {
        renderJson(R.ok().put("data", courseService.queryById(courseId)));
    }


    /**
     * 查询资源
     */
    @ActionApi(summary = "根据课程ID获取资源", remark="根据课程ID获取资源")
    @Params({
            @Param(name="courseId", remark="课程Id", dataType="Integer", defaultValue="", required=true)
    })
    @Permissions("kms:course:read")
    public void queryResourceList(@Para("courseId") Integer courseId) {
        renderJson(R.ok().put("data", courseService.listResourcesByCourseId(courseId)));
    }

//    /**
//     * 查询角色
//     */
//    @ActionApi(summary = "根据课程ID获取角色", remark="根据课程ID获取角色")
//    @Params({
//            @Param(name="courseId", remark="课程Id", dataType="Integer", defaultValue="", required=true)
//    })
//    public void queryRoleList(@Para("courseId") Integer courseId) {
////        renderJson(R.ok().put("data", crmProductService.listSpecByProductId(productId)));
//    }

}
